<template>
    <div class="BpmTaskStart">
        <rx-layout>
            <template v-slot:center>
                <RxLoading v-if="Loading"></RxLoading>
                <rx-form v-else-if="formJs.data" ref="rxForms" :action="action"
                         :context-data="contextData"
                         :formJs="formJs" :resForm="resForm"></rx-form>
            </template>
            <template v-slot:footer>
                <task-tool-bar ref="taskToolBar"  :cur-data="curData" :appId="appId" :bpmTaskDetail="bpmTaskDetail" :bpmActionType="'bpmTaskStart'" :taskConfig="taskConfig"
                               @showBpmCommu="showBpmCommu"/>
            </template>
        </rx-layout>
    </div>
</template>

<script>
import RxLayout from '@/components/RxLayout'
import RxForm from "../../../views/modules/form/customForm/RxForm"
import ProcessToolBar from "./ProcessToolBar";
import BpmtaskApi from "@/api/rxBpm/bpmTask";
import TaskToolBar from "./TaskToolBar"
import Util from "@/js/Util.js";
import RxLoading from '@/components/RxLoading'

export default {
    name: "BpmTaskStart",
    props: {
        taskId: {
            type: String,
            required: true
        },
        instId: {
            type: String,
            required: true
        },
        layerid: String,
        destroy: Function,
        appId: String,
    },
    data() {
        return {
            taskStatus: true,
            status: '',
            bpmTaskDetail: {},
            formJs: {},
            action: 'edit',//状态edit编辑，detail明细
            resForm: {},
            contextData: {},
            taskConfig: {},
            //根实例
            rootVm: true,
            Loading:true,
            curData:[],
        }
    },
    components: {
        RxForm,
        RxLayout,
        ProcessToolBar,
        TaskToolBar,
        RxLoading
    },
    created() {
        this.loadTaskInfo();
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"bpmTaskLang.bpmOther",key);
        },
        //刷新任务
        async loadTaskInfo() {
            let self = this;
            let res = await BpmtaskApi.getAllDetail(this.taskId);
            if (!res.success) {
                return;
            }
            self.Loading=false;
            //设置启动标题.
            uni.setNavigationBarTitle({
                title: res.data.bpmInst.subject
            });

            this.status = res.data.bpmInst.status;
            this.bpmTaskDetail = res.data;
            if (res.data.bpmTask == null) {
                uni.showToast({title:this.i18n('bpmTask','当前任务已经完成或删除，请刷新数据界面再操作！'), duration: 2000, icon: 'none'});
            } else {
                //当前实例为锁定 任务不是锁定 则显示为运行中
                if (this.status == 'LOCKED' && res.data.bpmTask.status != 'LOCKED') {
                    this.status = 'RUNNING';
                } else if (this.status == 'RUNNING' && res.data.bpmTask.status == 'LOCKED') {
                    //当前实例为运行中 任务是锁定 则显示为锁定
                    this.status = 'LOCKED';
                }
            }
            Object.assign(self, res.data);
            let curUser = uni.getStorageSync("user");
            let data = res.data;
            this.taskConfig = res.data.taskConfig;
            let formData = data.formData;
            this.contextData = {
                type: "usetask",
                curUserId: curUser.userId,
                curUserName: curUser.fullName,
                account: curUser.account,
                deptId: curUser.deptId,
                nodeId: self.bpmTask.key,
                nodeName: self.bpmTask.name,
                instId: self.instId,
                defId: self.bpmInst.defId,
                instNo: self.bpmInst.billNo,
                taskId: self.taskId,
                tenantId: curUser.tenantId,
                tenantLabel: curUser.tenantLabel,
                opinionHistorys: res.data.bpmCheckHistories,
                alias: this.taskConfig.alias,
                subject: this.taskConfig.subject,
                //当前是否为发起人
                starter:self.bpmInst.createBy==curUser.userId,
            };
            this.resForm = formData.data[0];
            if(this.resForm.globalConfig){
                uni.setStorage({key: "globalConfig", data: JSON.parse(this.resForm.globalConfig)})
            }else {
                uni.setStorage({key: "globalConfig", data: {}})
            }
            this.$nextTick(function () {
                self.curData = res.data.formData.data ;
            })
            let scriptJs = Util.replaceDefault(this.resForm.script);
            this.formJs = new Function('return ' + scriptJs)();
        },

        showBpmCommu(val) {
            this.bpmCommuShow = true;
        },
    }
}
</script>

<style scoped>
.BpmTaskStart {
    height: 100%;
}
</style>
